package com.hfkh.keyun.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Carousel;
import com.hfkh.keyun.entity.Course;

import com.hfkh.keyun.entity.Major;
import com.hfkh.keyun.entity.Unit;
import com.hfkh.keyun.exception.BusinessException;
import com.hfkh.keyun.mapper.CourseMapper;
import com.hfkh.keyun.mapper.MajorMapper;
import com.hfkh.keyun.mapper.UnitMapper;
import com.hfkh.keyun.service.CourseService;
import com.hfkh.keyun.util.PageDTO;

import com.hfkh.keyun.vo.CourseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Value("${reggie.path}}")
    private String basepath;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private UnitMapper mapper;
    @Resource
    private MajorMapper majorMapper;
    @Override
    public ApiResult addCourse(CourseVO courseVO) throws IOException, BusinessException {
        MultipartFile file =courseVO.getFile();
        if(file.isEmpty()){
            return ApiResult.fail("文件为空");
        }
        // 获取原始文件名
        String filename = file.getOriginalFilename();
        // 获取文件后缀名
        String suffixName = filename.substring(filename.lastIndexOf("."));
        //使用uuid唯一识别码与原来的文件名字组合构建新的名字
        String newFileName= UUID.randomUUID()+suffixName;
        String filePathSql = "http://localhost:8080/images/";
        String filePath = "D:/spring/demo/keyun/src/main/resources/static/images/";
        String path = filePath + newFileName;
        String pathSql = filePathSql + newFileName;
        File dest = new File(new File(path).getAbsolutePath());
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            //因此这里使用.getParentFile()，目的就是取文件前面目录的路径
            dest.getParentFile().mkdirs();// 新建文件夹
        }
        file.transferTo(dest); //文件写入
        Course course = new Course();
        course.setId(courseVO.getId());
        course.setCourseName(courseVO.getCourseName());
        course.setCourseDesc(courseVO.getCourseDesc());
        course.setCoursePrice(courseVO.getCoursePrice());
        course.setPicUrl(pathSql);
        course.setVisitCount(courseVO.getVisitCount());
        course.setCourseDetail(courseVO.getCourseDetail());
        course.setUnitId(courseVO.getUnitId());
        course.setMajorId(courseVO.getMajorId());
        if(courseVO.getId()==null){
            courseMapper.insert(course);
            return ApiResult.fail("新增成功");
        }
        courseMapper.updateById(course);
        return ApiResult.fail("修改成功!");
    }
    @Override
    public ApiResult queryPage(Integer id, Integer page, Integer size) {
        Page<Course> itemPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("id",id);
        queryWrapper.orderByDesc("create_date");
        Page result = courseMapper.selectPage(itemPage, queryWrapper);
        PageDTO<Carousel> pageDTO = new PageDTO<>();
        pageDTO.setTotal(result.getTotal());
        pageDTO.setList(result.getRecords());
        return ApiResult.success(pageDTO);
    }
    @Override
    public Page<Course> selectCourseAndUnitPage(Page<Course> page, Integer majorId) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        Page<Course> coursePage = courseMapper.selectPage(page, queryWrapper);
        return coursePage;
    }


}
